package com.yi.mall.cart.Controller;

import com.yi.mall.common.model.Cart;
import com.yi.mall.cart.service.ICartService;
import com.yi.mall.common.utils.ResponseEnum;
import com.yi.mall.common.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping(value = "/cart")
public class CartController {
    @Autowired
    private ICartService cartService;

    /**
     * 添加购物车
     * id: skuid
     */
    @PostMapping("/save")
    public Result add(@RequestBody Cart cart){
        // 购物车编号:由用户编号和商品编号组成
        String id = cart.getUserId() + cart.getSkuId();
        cart.set_id(id);
        // 创建时间
        cart.setCreateTime(LocalDateTime.now());
        //加入购物车
        cartService.add(cart);
        return Result.success(ResponseEnum.SUCCESS);
    }

    /**
     * 购物车列表
     *
     * @return
     */
    @GetMapping(value = "/list/{userId}")
    public Result<List<Cart>> list(@PathVariable String userId) {
        List<Cart> carts = cartService.list(userId);
        return Result.success(ResponseEnum.SUCCESS,carts);
    }

    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable String id) {
        cartService.removeById(id);
        return Result.success(ResponseEnum.SUCCESS);
    }

    @DeleteMapping("/rmall")
    public Result rmall(@RequestParam("ids") List<String>ids) {
        cartService.removeAllById(ids);
        return Result.success(ResponseEnum.SUCCESS);
    }
    @GetMapping("/findByid")
    public Result findById(@RequestParam("id") String id) {
        return Result.success(ResponseEnum.SUCCESS,cartService.getById(id));
    }
}
